﻿Imports System.Data.OleDb

Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            bindYear()      'Bind the year, make the ddl_year to show the information of year
            bindMonth()     'Bind the month. make the ddl_month to show the inforation of month
            bindDay()       'Bind the day, make the ddl_day to show the information of day

            'The following statement is to load the information of user profile, and to show those information in the text box
            Dim sql As String = "SELECT * FROM UserProfile WHERE dinerID=@f1"        'This is an SQL statment
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SpiritConnString").ConnectionString)       'Connect to the Database
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            conn.Open()
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            Dim datebaseReader = cmd.ExecuteReader()

            datebaseReader.Read()

            If Not IsDBNull(datebaseReader("firstName")) Then
                tb_firstName.Text = datebaseReader("firstName")
            End If

            If Not IsDBNull(datebaseReader("lastName")) Then
                tb_lastName.Text = datebaseReader("lastName")
            End If

            If Not IsDBNull(datebaseReader("gender")) Then
                ddl_gender.SelectedValue = datebaseReader("gender")
            End If

            If Not IsDBNull(datebaseReader("nationality")) Then
                tb_nationality.Text = datebaseReader("nationality")
            End If

            If Not IsDBNull(datebaseReader("dateOfBirth")) Then
                Dim dateOfBirth As DateTime = datebaseReader("dateOfBirth")
                ddl_year.SelectedValue = dateOfBirth.Year
                ddl_month.SelectedValue = dateOfBirth.Month
                ddl_day.SelectedValue = dateOfBirth.Day
            End If

            If Not IsDBNull(datebaseReader("myDescription")) Then
                tb_myDescription.Text = datebaseReader("myDescription")
            End If

            datebaseReader.Close()
            conn.Close()

        End If

    End Sub

    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click

        Dim newPictureName As String = ""

        'Give the new picture a random name
        If fup_protrait.HasFile Then
            newPictureName = Guid.NewGuid().ToString() & getFormate(fup_protrait.FileName)       'Give the random name, and it will not change the suffix of that image file
            If fup_protrait.HasFile Then
                fup_protrait.SaveAs(Server.MapPath("Pictures") & "/" & newPictureName)           'Put the picture in the Pictures Folder
            End If
        End If

        Dim dateOfBirth As DateTime = New DateTime(CInt(ddl_year.SelectedValue), CInt(ddl_month.SelectedValue), CInt(ddl_day.SelectedValue))

        Dim sql As String
        If fup_protrait.HasFile Then
            sql = "UPDATE UserProfile SET firstName=@f1, lastName=@f2, gender=@f3, nationality=@f4, protrait=@f5, dateOfBirth=@f6, myDescription=@f7 WHERE dinerID=@f8"        'This is an SQL statment
        Else
            sql = "UPDATE UserProfile SET firstName=@f1, lastName=@f2, gender=@f3, nationality=@f4, dateOfBirth=@f6, myDescription=@f7 WHERE dinerID=@f8"
        End If
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("SpiritConnString").ConnectionString)       'Connect to the Database
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        conn.Open()
        cmd.Parameters.AddWithValue("@f1", tb_firstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_lastName.Text)
        cmd.Parameters.AddWithValue("@f3", ddl_gender.SelectedValue)
        cmd.Parameters.AddWithValue("@f4", tb_nationality.Text)
        If fup_protrait.HasFile Then
            cmd.Parameters.AddWithValue("@f5", "Pictures/" & newPictureName)
        End If
        cmd.Parameters.AddWithValue("@f6", dateOfBirth)
        cmd.Parameters.AddWithValue("@f7", tb_myDescription.Text)
        cmd.Parameters.AddWithValue("@f8", User.Identity.Name)

        cmd.ExecuteNonQuery()

        conn.Close()

        Response.Redirect("nonLgoinMyProfile.aspx")

    End Sub

    'This function can allow user to upload any formate of a picture
    Function getFormate(ByVal fileName As String)
        Dim fileParts = fileName.Split(".")         'Break up string by '.'
        Dim formate = fileParts(fileParts.Length - 1)       'Get the string after the last '.'   In other word, this will get the suffix of the file name
        Return "." & formate
    End Function

    'I use 'Sub' not the 'Function', because there is no return value
    'This Sub can bind the date
    Sub bindYear()
        Dim startYear As Integer = DateTime.Now.Year - 100
        Dim currentYear As Integer = DateTime.Now.Year

        'Use a loop to bind lasted 100 year into the 'ddl_year'
        For i As Integer = startYear To currentYear
            ddl_year.Items.Add(New ListItem(i.ToString()))
        Next
    End Sub

    'This Sub is to bind the month into the ddl_month
    Sub bindMonth()

        'use a loop to bind 12 monther into the ddl_month
        For i As Integer = 1 To 12
            ddl_month.Items.Add(i.ToString())
        Next
    End Sub

    'This Sub is to bind the day into the ddl_day
    Sub bindDay()

        Dim year As String = ddl_year.SelectedValue
        Dim month As String = ddl_month.SelectedValue

        'This statement is to check how many days in the month of the year
        Dim days As Integer = DateTime.DaysInMonth(CInt(year), CInt(month))
        For i As Integer = 1 To days
            ddl_day.Items.Add(i.ToString())
        Next
    End Sub

    Sub ddl_year_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddl_year.SelectedIndexChanged

        'When the value of ddl_year is changed, it will invoke the 'bindDay()' method.
        bindDay()

    End Sub

    Sub ddl_month_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddl_month.SelectedIndexChanged

        'When the value of ddl_month list is changed, it will invoke the 'bindDay()' method.
        bindDay()

    End Sub
End Class